﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace KOPLibrary.Controls
{
    public partial class BarcodeParametersControl : UserControl
    {
        public BarcodeParametersControl()
        {
            InitializeComponent();
            InitUI();
            Settings = BarcodeParameters.Empty;
        }

        private void BarcodeParametersControl_Load(object sender, EventArgs e)
        {
                       
        }

        public BarcodeParameters Settings
        {
            get
            {
                BarcodeParameters result = new BarcodeParameters()
                {
                    BarcodeType = (BarcodeLib.TYPE)Enum.Parse(typeof(BarcodeLib.TYPE), ctlBarcodeType.SelectedItem.ToString()),
                    Width = int.Parse(ctlWidth.Text),
                    Height = int.Parse(ctlHeight.Text),
                    IncludeText = ctlIncludeData.Checked
                };
                return result;
            }

            set
            {
                ctlBarcodeType.SelectedItem = value.BarcodeType.ToString();
                ctlWidth.Text = value.Width.ToString();
                ctlHeight.Text = value.Height.ToString();
                ctlIncludeData.Checked = value.IncludeText;
            }
        }

        private void InitUI()
        {
            string[] types = Enum.GetNames(typeof(BarcodeLib.TYPE));
            foreach (string x in types)
            {
                if (x != "UNSPECIFIED")
                    ctlBarcodeType.Items.Add(x);
            }
            ctlBarcodeType.SelectedIndex = 0;
        }


        private bool IsNumber(int key)
        {
            if ((key >= 48 && key <= 57) || (key >= 96 && key <= 105) || key == 8) return true; else return false;
        }

        bool widthisnumber = false;
        bool heightisnumber = false;

        private void ctlWidth_KeyDown(object sender, KeyEventArgs e)
        {
            widthisnumber = IsNumber(e.KeyValue);
        }

        private void ctlWidth_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !widthisnumber;
        }

        private void ctlHeight_KeyDown(object sender, KeyEventArgs e)
        {
            heightisnumber = IsNumber(e.KeyValue);
        }

        private void ctlHeight_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !heightisnumber;
        }
    }

    public class BarcodeParameters
    {
        public BarcodeLib.TYPE BarcodeType { get; set; }
        public int Width { get; set; }
        public int Height { get; set; }
        public bool IncludeText { get; set; }

        public static BarcodeParameters Empty
        {
            get
            {
                return new BarcodeParameters()
                {
                    BarcodeType = BarcodeLib.TYPE.CODE39,
                    Width = 200,
                    Height = 100,
                    IncludeText = true
                };
            }
        }
    }
}
